package br.com.bacabeleireiros.util.persistence.impl;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import br.com.bacabeleireiros.util.persistence.GenericDAO;

@Repository
public class GenericDAOImpl<T, I extends Serializable> implements GenericDAO<T, I> {
	
	@PersistenceContext
	protected EntityManager em;
	
    public T save(T entity) {
    	T saved = null;
		saved = em.merge(entity);
        return saved;
    }
	
	public void remove(T entity) {
		em.remove(entity);
		
	}

	public T getById(Class<T> classe, I pk) {
		return em.find(classe, pk);
		
	}

	public List<T> getAll(Class<T> classe) {
		return em.createQuery("select o from " + classe.getSimpleName() + " o order by 1").getResultList();
	}
	
}